import { useReducer } from 'react';

const initialState = { count: 0 };

function init(initialCount) {
    return { count: initialCount };
}
function reducer(state, action) {
    // eslint-disable-next-line default-case
    switch (action.type) {
        case 'in':
            return { count: state.count + 1 };
        case 'de':
            return { count: state.count - 1 };
        case 'reset':
            return init(action.payload);
        default:
            throw new Error();
    }
}

function Counter(initialCount) {
    const [state, dispatch] = useReducer(reducer, initialCount, init);

    return (
        <>
            Count: {state.count}
            <button onClick={() => dispatch({ type: 'reset', payload: initialCount })}>Reset</button>
            <button onClick={() => dispatch({ type: 'de' })}>-</button>
            <button onClick={() => dispatch({ type: 'in' })}>+</button>
        </>
    );
}
export default Counter;
